package com.manage.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.manage.demo.dao.model.TeacherInfo;
import com.manage.demo.service.StudentService;
import com.manage.demo.service.TeacherService;
import com.manage.demo.dao.model.ClassInfo;

@Controller
public class teacherController {
	@Autowired
	private TeacherService TeacherService;
	@Autowired
	private StudentService StudentService;
	
	@RequestMapping(value="/teacher", method=RequestMethod.GET)
	public String login(HttpServletRequest request, Model model){
		model.addAttribute("course", TeacherService.getcourse());
		model.addAttribute("name", request.getSession().getAttribute("name"));
		model.addAttribute("perm", request.getSession().getAttribute("perm"));
		model.addAttribute("admin", request.getSession().getAttribute("admin"));
		model.addAttribute("user_input", request.getSession().getAttribute("user_input"));
		System.out.println(model);
        return "/html/teacher";
	} 
	
	@RequestMapping(value="/search", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> search(HttpServletRequest request, @RequestParam(value = "subject", required = true) String subject, @RequestParam(value = "id", required = true) String id, @RequestParam(value = "limit", required = true) int limit, @RequestParam(value = "offset", required = true) int offset) {
		Map<String, Object> res = new HashMap<String, Object>();
		String user_input = (String) request.getSession().getAttribute("user_input");
		String perm = (String) request.getSession().getAttribute("perm");
		String admin = (String) request.getSession().getAttribute("admin");
		if(perm.equals("0")) {
            String newgrade = StudentService.getStudentListByCondition("", "", "", user_input,1,0).get(0).getGrade();
            String newclassname = StudentService.getStudentListByCondition("", "", "", user_input,1,0).get(0).getClass_num();
            List<String> searchList = StudentService.getGradeList(newgrade, newclassname);
            if(id.equals("")) {
            	List<Map<String, Object>> teacherList = new ArrayList<Map<String, Object>>();
            	for(String item : searchList) {
            		teacherList.addAll(TeacherService.getTeacherListByCondition(subject, item, 5, 0));
                }
            	res.put("data", teacherList);
            	res.put("count", teacherList.size());
            	
            	
            }else if(searchList.contains(id)) {
            	res.put("data", TeacherService.getTeacherListByCondition(subject, id, limit, offset));
            	res.put("count", TeacherService.getCount(subject, id));
            }else {
            	res.put("data", TeacherService.getTeacherListByCondition("0", "0", limit, offset));
            	res.put("count", TeacherService.getCount("0", "0"));
            }
        }else{
        	res.put("data", TeacherService.getTeacherListByCondition(subject, id, limit, offset));
        	res.put("count", TeacherService.getCount(subject, id));

        }
		return res;
	}
	
	@RequestMapping(value="/getadministrator", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getadministrator(HttpServletRequest request, @RequestParam(value = "id", required = true) String id) {
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("data", TeacherService.getAdministratorByCondition(id));
		return res;
	}
	
	@RequestMapping(value="/revisesearch", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getTeacherMange(HttpServletRequest request, @RequestParam(value = "id", required = true) String id) {
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("data", TeacherService.getTeacherMange(id));
		return res;
	}
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(HttpServletRequest request, @RequestParam(value = "id", required = true) Integer id, @RequestParam(value = "name", required = true) String name, @RequestParam(value = "sex", required = true) String sex, @RequestParam(value = "tel", required = true) String tel, @RequestParam(value = "subject", required = true) String subject, @RequestParam(value = "administrator", required = true) String administrator) {
		Map<String, Object> res = new HashMap<String, Object>();
		List<Integer> idlist = new ArrayList<Integer>();
		String newid = "" + id;
		
		for(Integer item : TeacherService.getTeacherIdByCondition(id)) {
			idlist.add(item);
		};
		Integer size = idlist.size();
		if(size.equals(0)) {
			newid = newid.concat("01");
		}else {
			Integer newid1 = idlist.stream().max(Integer::compareTo).get();
			newid1 = newid1 + 1;
			newid = "" + newid1;
		}
		TeacherService.setTeacherByCondition(newid, name, sex, tel, subject);
		TeacherService.setUserByCondition(newid, administrator);
		
		res.put("code", "200");
		res.put("msg", "添加成功！");
		
		return res; 
	}

	@RequestMapping(value="/revise", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> revise(HttpServletRequest request,@RequestParam(value = "id", required = true) String id, @RequestParam(value = "name", required = true) String name, @RequestParam(value = "sex", required = true) String sex, @RequestParam(value = "tel", required = true) String tel, @RequestParam(value = "subject", required = true) String subject, @RequestParam(value = "administrator", required = true) String administrator) {
		Map<String, Object> res = new HashMap<String, Object>();
		Integer ret = TeacherService.update(id, name, sex, tel, subject);
		TeacherService.updateUser(id, administrator);
		res.put("code", "200");
		res.put("msg", "修改成功！");
		res.put("data", ret);
		return res;
	}
	
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request,@RequestParam(value = "id", required = true) String id) {
		Map<String, Object> res = new HashMap<String, Object>();
		Integer ret = TeacherService.delete(id);
		TeacherService.deleteUser(id);
		res.put("code", "200");
		res.put("msg", "删除成功！");
		res.put("data", ret);
		return res;
	}
}
	
